Ext.define('DragonApp.view.channel.ChannelController', {
    extend: 'DragonApp.view.foundation.ViewController',

    alias: 'controller.channel',

    createDialog: function(record) {

        var me = this,
            view = me.getView();

        me.dialog = view.add({
            xtype: 'channel_form',
            height: 250,
            width: 400,

            viewModel: {
                data: {
                    title: record ? '编辑渠道' : '新增渠道'
                }
            }
        });
        if (!Ext.isEmpty(record)) {
            me.dialog.down("form").loadRecord(record);
        }

        me.dialog.show(); //窗体显示
    },

    onSave: function(button, e, options) {

        var window = this.lookupReference('channel_window');

        var form = this.lookupReference('channel_form').getForm();
        var formValue = form.getValues();

        if (Ext.isEmpty(formValue.id)) {

            var url = DragonApp.ux.config.Config.getUrl('/index/Channel/doJsonPAdd/');
        } else {

            var url = DragonApp.ux.config.Config.getUrl('/index/Channel/doJsonPUpdate/');
        }

        var me = this;
        var store = Ext.data.StoreManager.lookup('jsonPChannelStore');

        window.mask('提交中...');

        if (form.isValid()) {

            Ext.data.JsonP.request({
                url: url,
                async: false,
                params: formValue,
                callbackKey: "callback",
                success: function(result) {

                    window.unmask();
                    DragonApp.util.Msg.showToast(result.msg);

                    switch (result.success) {
                        case true:
                            form.destroy();
                            window.destroy();
                            store.loadPage(1);
                            break;
                    }

                },
                failure: function(result) {
                    window.unmask();
                    Ext.Msg.alert('提示', result.msg);
                }
            });
        }
    },

    onDelete: function(button, e, options) {

        var record = button.getWidgetRecord();

        var formValue = {};

        formValue.id = record.get('id');

        var store = Ext.data.StoreManager.lookup('jsonPChannelStore');

        Ext.Msg.show({

            title: '提示',
            msg: '删除这条记录?',
            buttons: Ext.Msg.YESNO,
            icon: Ext.Msg.QUESTION,

            fn: function(buttonId) {

                if (buttonId == 'yes') {

                    Ext.data.JsonP.request({

                        url: DragonApp.ux.config.Config.getUrl('/index/Channel/doJsonPDelete/'),
                        async: false,
                        params: formValue,
                        callbackKey: "callback",
                        success: function(result) {

                            DragonApp.util.Msg.showToast(result.msg);

                            switch (result.success) {
                                case true:
                                    store.loadPage(1);
                                    break;
                            }
                        },
                        failure: function(result) {
                            Ext.Msg.alert('提示', result);
                        }
                    });
                }
            }
        });
    },

    onRefresh: function(button, e, options) {

        var store = Ext.data.StoreManager.lookup('jsonPChannelStore');
        store.loadPage(1);
    },

    //============
    //渠道状态转换
    //============
    activeStatusRender: function(value) {
        switch (value) {
            case 0:
                return '未激活'
                break;
            case 1:
                return '激活'
                break;
        }
    }

});